package chain_to_chain

import (
	"chain_bridge_service/internal/repository/mongodb/user"
	"chain_bridge_service/internal/services/chain_to_chain"
	"net/http"

	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
)

type listRequest struct {
	PageIndex uint   `bson:"pageIndex"` // 第几页
	PageSize  uint   `bson:"pageSize"`  // 每页显示条数
	UserId    string `json:"userId"`
}

type Repose struct {
	Id             string `json:"id"`
	UserId         string `json:"userId"`
	ChainConfigId0 string `json:"chainConfigId0"`
	ChainConfigId1 string `json:"chainConfigId1"`
	Token0         string `json:"token0"`
	Token1         string `json:"token1"`
	ChainName0     string `json:"chainName0"`
	ChainName1     string `json:"chainName1"`
	Symbol0        string `json:"symbol0"`
	Symbol1        string `json:"symbol1"`
	Rate           string `json:"rate"`
}

type Page struct {
	Total int64       `json:"total"`
	List  interface{} `json:"list"`
}

func (h *handler) List() core.HandlerFunc {
	return func(ctx core.Context) {

		req := new(listRequest)
		res := new(createResponse)

		if err := ctx.ShouldBindQuery(&req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		userInfo := ctx.GetInfo("userInfo").(*user.User)
		createData := new(chain_to_chain.CreateChainData)
		createData.UserId = userInfo.Id
		productList, err := h.chainService.List(ctx, req.PageIndex, req.PageSize, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetProductListError,
				code.Text(code.GetProductListError)).WithError(err),
			)
			return
		}
		var reposes []Repose

		for _, value := range productList {

			var repose Repose

			configData := new(chain_to_chain.CreateChainData)
			configData.ChainConfigId0 = value.ChainConfigId0
			configData.ChainConfigId1 = value.ChainConfigId1
			configobj, err := h.chainService.Find(ctx, configData)
			if err != nil {
				ctx.AbortWithError(core.Error(
					http.StatusOK,
					code.GetProductListError,
					code.Text(code.GetProductListError)).WithError(err),
				)
				return
			}
			for _, cid := range configobj {

				switch {

				case cid.Id == value.ChainConfigId0:
					repose.ChainName0 = cid.ChainName
				case cid.Id == value.ChainConfigId1:
					repose.ChainName1 = cid.ChainName
				}

			}
			tokenData := new(chain_to_chain.CreateChainData)
			tokenData.Token0 = value.Token0
			tokenData.Token1 = value.Token1
			tokenobj, err := h.chainService.FindToken(ctx, tokenData)
			if err != nil {
				ctx.AbortWithError(core.Error(
					http.StatusOK,
					code.GetProductListError,
					code.Text(code.GetProductListError)).WithError(err),
				)
				return
			}
			for _, tid := range tokenobj {

				switch {
				case tid.Id == value.Token0:
					repose.Symbol0 = tid.Symbol
				case tid.Id == value.Token1:

					repose.Symbol1 = tid.Symbol
				}

			}
			repose.UserId = value.UserId
			repose.ChainConfigId0 = value.ChainConfigId0
			repose.ChainConfigId1 = value.ChainConfigId1
			repose.Token0 = value.Token0
			repose.Token1 = value.Token1
			repose.Id = value.Id
			repose.Rate = value.Rate.String()
			reposes = append(reposes, repose)

		}

		count, err := h.chainService.Count(ctx, createData)
		page := new(Page)
		page.Total = count
		page.List = reposes

		res.Code = code.StatusOK
		res.Data = page
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
